package com.xiaoyu.util2;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SecUtil {
	public static MessageDigest MD5 = null;

	public static String FileMD5(File file) {
		FileInputStream fileInputStream = null;
		Integer length;
		try {
			fileInputStream = new FileInputStream(file);
			byte[] buffer = new byte[8192];
			while ((length = fileInputStream.read(buffer)) != -1) {
				MD5.update(buffer, 0, length);
			}
			String str = new BigInteger(1, MD5.digest()).toString(16);
			return str;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if (fileInputStream != null)
					fileInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static String md5(String password) {
		try {
			MD5.update(password.getBytes());
			String pwd = new BigInteger(1, MD5.digest()).toString(16);
			return pwd;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return password;
	}

	static {
		try {
			MD5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException ne) {
			ne.printStackTrace();
		}
	}
}